/*
 * @Date: 2020-08-25 16:35:07
 * @LastEditors: xujianxing
 * @LastEditTime: 2020-08-25 17:10:34
 * @FilePath: \nili\src\common\guard\role.guard.ts
 */
import { Injectable, CanActivate, ExecutionContext } from '@nestjs/common';
import { Observable } from 'rxjs';
import { Reflector } from '@nestjs/core';

@Injectable()
export class RolesGuard implements CanActivate {
  constructor(private reflector: Reflector) {}

  canActivate(
    context: ExecutionContext,
  ): boolean | Promise<boolean> | Observable<boolean> {
    // 通过注解名拿到，注解参数
    const roles = this.reflector.get<string[]>('roles', context.getHandler());
    const request = context.switchToHttp().getRequest();
    const user = request.body;
    /** 这里根据得到的角色进行匹配，不符合则抛出异常 */
    return true;
  }
}
